package com.lou1052.kryo;

import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2015/11/3
 * Time: 22:44
 * To change this template use File | Settings | File Templates.
 */
public class KryoExtPoolQueueImpl implements  KryoExtPool{
    private final Queue<KryoExt> queue;
    private final KryoExtFactory factory;

    public KryoExtPoolQueueImpl(KryoExtFactory factory, Queue<KryoExt> queue) {
        this.factory = factory;
        this.queue = queue;
    }

    public int size () {
        return queue.size();
    }

    public KryoExt borrow () {
        KryoExt res;
        if((res = queue.poll()) != null) {
            return res;
        }
        return factory.create();
    }

    public void release (KryoExt kryo) {
        queue.offer(kryo);
    }


    public void clear() {
        queue.clear();
    }
}
